package cn.org.mrliu.code.generator;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;

import cn.org.mrliu.code.generator.util.ConfigurationUtil;
import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;

/**
 * Map<String, Object> root templateName outDir
 * 
 * @author MrLiu
 *
 */
public class Generator {
	private static Configuration freemarkerCfg;

	static {
		freemarkerCfg = ConfigurationUtil.getConfiguration();
	}

	private Generator() {

	}

	private static void validate(String templateName, Map<String, Object> root) {
		if (templateName == null || templateName.trim().length() == 0) {
			throw new RuntimeException("模板名称不能为空!");
		}
		if (root == null || root.size() == 0) {
			throw new RuntimeException("模板数据不能为空!");
		}
	}

	public static void generate(String templateName, Map<String, Object> root) {
		generate(templateName, root, null);
	}

	public static void generate(String templateName, Map<String, Object> root, File outFile) {
		generate(templateName, root, outFile, true);
	}

	public static void generate(String templateName, Map<String, Object> root, File outFile, boolean replace) {
		validate(templateName, root);
		if (outFile == null) {
			outConsole(templateName, root);
		} else {
			outTemplate(templateName, root, outFile, replace);
		}
	}

	private static void outTemplate(String templateName, Map<String, Object> root, File outFile, boolean replace) {
		try {
			// 如果文件存在,跳过属性为true,则跳过,不覆盖内容
			if (outFile.exists() && !replace) {
				System.out.println("跳过" + outFile);
				return;
			}
			// 如果文件不存在,则创建
			File parent = outFile.getParentFile();
			if (parent != null && !parent.exists()) {
				parent.mkdirs();
			}
			// outFile.createNewFile();
			System.out.println("templateName:"+templateName);
			Template temp = freemarkerCfg.getTemplate(templateName);
			OutputStream outputStream = new FileOutputStream(outFile);
			Writer out = new OutputStreamWriter(outputStream);
			temp.process(root, out);
			System.out.println("out:"+outFile);
			System.out.println("out path:"+outFile.getAbsolutePath());
		} catch (TemplateNotFoundException e) {
			e.printStackTrace();
		} catch (MalformedTemplateNameException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
	}

	private static void outConsole(String templateName, Map<String, Object> root) {
		try {
			Template temp = freemarkerCfg.getTemplate(templateName);
			Writer out = new OutputStreamWriter(System.out);
			temp.process(root, out);
		} catch (TemplateNotFoundException e) {
			e.printStackTrace();
		} catch (MalformedTemplateNameException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
	}
}
